﻿//F# の詳細 (http://fsharp.net)

open System
open System.IO
open PcapDotNet.Core
open PcapDotNet.Packets
open PcapDotNet.Packets.Transport

open MySQLQueryMonitorModule

let main() =
    let selectedDevice = LivePacketDevice.AllLocalMachine.[0]

    // デバイスをオープン
    let port = 3306us
    let monitor = new MySQLQueryMonitor(selectedDevice, port, 1000)
    while true do
        match monitor.Receive() with
        | null -> ()
        | packet ->
            try
                let timeStamp = packet.Timestamp.ToString("hh:mm:ss.fff")
                let checkSum = packet.Ethernet.IpV4.Tcp.Checksum
                match packet.Ethernet.IpV4.Tcp with
                | tcp when tcp.PayloadLength < 5      -> ()
                | tcp when tcp.DestinationPort = port -> parseMySQLCommandPacket packet |> printfn "%A %X\t%A" timeStamp checkSum
                | tcp when tcp.SourcePort = port      -> parseMySQLResultPacket  packet |> printfn "%A %X\t%A" timeStamp checkSum
                | _ -> ()
            with
                msg -> printfn "%A" msg

main()

